/*******************************************************************************
 * Copyright (c) 2001, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/*******************************************************************************
 *
 * AddToneMarks.java
 *
 * Dec 10, 2006 8:56:57 PM
 *
 ******************************************************************************/

package com.eastmountain.chinese.tonemarks;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;

import com.eastmountain.chinese.util.FormatPinYin;


/*******************************************************************************
 *
 * AddToneMarks
 *
 ******************************************************************************/

public class AddToneMarks {

    private JFrame    jFrame                = null;

    private JPanel    jContentPane          = null;

    private JMenuBar  jMenuBar             = null;

    private JMenu     fileMenu              = null;

    private JMenu     editMenu              = null;

    private JMenu     helpMenu              = null;

    private JMenuItem exitMenuItem          = null;

    private JMenuItem aboutMenuItem         = null;

    private JMenuItem cutMenuItem           = null;

    private JMenuItem copyMenuItem          = null;

    private JMenuItem pasteMenuItem         = null;

    private JMenuItem saveMenuItem          = null;

    private JDialog   aboutDialog           = null;

    private JPanel    aboutContentPane      = null;

    private JLabel    aboutVersionLabel     = null;

    private JPanel    dataPanel             = null;

    private JPanel    buttonPanel           = null;

    private JButton   okButton              = null;

    private JLabel    numberedPinYinLabel   = null;

    private JLabel    toneMarkedPinYinLabel = null;

    private JScrollPane numberedPinYinScrollPane = null;

    private JScrollPane toneMarkedPinYinScrollPane = null;

    private JTextArea numberedPinYinField = null;

    private JTextArea toneMarkedPinYinField = null;

    private FontMetrics fontMetrics;

    protected static Border bevelBorder       = new BevelBorder(
                                                      BevelBorder.LOWERED);

    private static int TEXT_BORDER_INSET = 5;

    protected static Border emptyBorder       = new EmptyBorder(
                                                      AddToneMarks.TEXT_BORDER_INSET,
                                                      AddToneMarks.TEXT_BORDER_INSET,
                                                      AddToneMarks.TEXT_BORDER_INSET,
                                                      AddToneMarks.TEXT_BORDER_INSET);

    protected static Border compoundBorder    = new CompoundBorder(AddToneMarks.bevelBorder,
                                                      AddToneMarks.emptyBorder);


    /**
     *
     */
    public AddToneMarks() {

        // TODO Auto-generated constructor stub
    }

    /**
     * This method initializes dataPanel
     *
     * @return javax.swing.JPanel
     */
    private JPanel getDataPanel() {

        if (dataPanel == null) {

            GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
            gridBagConstraints3.fill = GridBagConstraints.BOTH;
            gridBagConstraints3.gridy = 0;
            gridBagConstraints3.weightx = 1.0;
            gridBagConstraints3.ipady = 40;
            gridBagConstraints3.gridheight = 3;
            gridBagConstraints3.insets = new Insets(10, 5, 5, 10);
            gridBagConstraints3.gridx = 1;

            GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
            gridBagConstraints4.fill = GridBagConstraints.BOTH;
            gridBagConstraints4.gridy = 3;
            gridBagConstraints4.weightx = 1.0;
            gridBagConstraints4.ipady = 40;
            gridBagConstraints4.gridheight = 3;
            gridBagConstraints4.insets = new Insets(6, 5, 10, 10);
            gridBagConstraints4.gridx = 1;

            GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
            gridBagConstraints1.gridx = 0;
            gridBagConstraints1.gridy = 2;
            numberedPinYinLabel = new JLabel();
            numberedPinYinLabel.setText( "Numbered PinYin" );
            numberedPinYinLabel.setHorizontalTextPosition(SwingConstants.RIGHT);
            numberedPinYinLabel.setPreferredSize(new Dimension(109, 16));
            numberedPinYinLabel.setMaximumSize(new Dimension(109, 16));
            numberedPinYinLabel.setMinimumSize(new Dimension(109, 16));
            numberedPinYinLabel.setHorizontalAlignment(SwingConstants.RIGHT);

            GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
            gridBagConstraints2.gridx = 0;
            gridBagConstraints2.gridy = 5;
            toneMarkedPinYinLabel = new JLabel();
            toneMarkedPinYinLabel.setText( "ToneMarked PinYin" );
            toneMarkedPinYinLabel.setHorizontalTextPosition(SwingConstants.RIGHT);
            toneMarkedPinYinLabel.setHorizontalAlignment(SwingConstants.RIGHT);

            dataPanel = new JPanel();
            dataPanel.setLayout( new GridBagLayout() );

            dataPanel.setBackground( new Color( 204, 204, 255 ) );
            dataPanel.setBorder( BorderFactory
                            .createBevelBorder( BevelBorder.LOWERED ) );

            dataPanel.setPreferredSize( new Dimension( 500, 200 ) );
            dataPanel.setMaximumSize( new Dimension( 500, 200 ) );
            dataPanel.setMinimumSize( new Dimension( 500, 200 ) );
            dataPanel.add( numberedPinYinLabel, gridBagConstraints1 );
            dataPanel.add( toneMarkedPinYinLabel, gridBagConstraints2 );

            dataPanel.add(getNumberedPinYinScrollPane(), gridBagConstraints3);
            dataPanel.add(getToneMarkedPinYinScrollPane(), gridBagConstraints4);
        }
        return dataPanel;
    }

    /**
     * This method initializes buttonPanel
     *
     * @return javax.swing.JPanel
     */
    private JPanel getButtonPanel() {

        if (buttonPanel == null) {
            GridBagConstraints gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 0;
            buttonPanel = new JPanel();
            buttonPanel.setLayout( new GridBagLayout() );
            buttonPanel.setBackground( new Color( 205, 205, 255 ) );
            buttonPanel.setPreferredSize( new Dimension( 400, 40 ) );
            buttonPanel.setMinimumSize( new Dimension( 400, 40 ) );
            buttonPanel.setMaximumSize( new Dimension( 400, 40 ) );

            buttonPanel.setBorder( BorderFactory
                            .createBevelBorder( BevelBorder.LOWERED ) );
            buttonPanel.add( getOkButton(), gridBagConstraints );

            // TODO add ENTER listener
            // TODO add CLEAR button

        }
        return buttonPanel;
    }

    /**
     * This method initializes okButton
     *
     * @return javax.swing.JButton
     */
    private JButton getOkButton() {

        if (okButton == null) {
            okButton = new JButton();
            okButton.setText( "OK" );
            okButton.addActionListener( new ButtonAction() );
        }
        return okButton;
    }

    /**
     * This method initializes numberedPinYinScrollPane
     *
     * @return javax.swing.JScrollPane
     */

    private JScrollPane getNumberedPinYinScrollPane() {

        if (numberedPinYinScrollPane == null) {

            numberedPinYinScrollPane = new JScrollPane(getNumberedPinYinField(),
                                                       ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                                                       ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);


        }

        return numberedPinYinScrollPane;
    }

    /**
     * This method initializes toneMarkedPinYinScrollPane
     *
     * @return javax.swing.JScrollPane
     */

    private JScrollPane getToneMarkedPinYinScrollPane() {

        if (toneMarkedPinYinScrollPane == null) {
            toneMarkedPinYinScrollPane = new JScrollPane(getToneMarkedPinYinField(),
                                                       ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                                                       ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            //numberedPinYinScrollPane.setViewportView(getNumberedPinYinField());
        }
        return toneMarkedPinYinScrollPane;
    }

    /**
     * This method initializes numberedPinYinField
     *
     * @return javax.swing.JTextArea
     */
    private JTextArea getNumberedPinYinField() {

        if (numberedPinYinField == null) {
            numberedPinYinField = new JTextArea();
            numberedPinYinField.setLineWrap(true);
            numberedPinYinField.setWrapStyleWord(true);

            Font font = new Font( "Courier New", Font.PLAIN, 16 );
            //fontMetrics = numberedPinYinField.getFontMetrics( font );
            numberedPinYinField.setFont( font );

            numberedPinYinField.setBackground(new Color(219, 219, 159));
            //numberedPinYinField.setMaximumSize(new Dimension(276, 20));

            numberedPinYinField.setBorder( AddToneMarks.compoundBorder );

        }
        return numberedPinYinField;
    }

    /**
     * This method initializes toneMarkedPinYinField
     *
     * @return javax.swing.JTextField
     */
    private JTextArea getToneMarkedPinYinField() {

        if (toneMarkedPinYinField == null) {
            toneMarkedPinYinField = new JTextArea();
            toneMarkedPinYinField.setLineWrap(true);
            toneMarkedPinYinField.setWrapStyleWord(true);

            Font font = new Font( "Courier New", Font.PLAIN, 16 );
            //fontMetrics = numberedPinYinField.getFontMetrics( font );
            toneMarkedPinYinField.setFont( font );

            toneMarkedPinYinField.setBackground(new Color(219, 219, 159));
            toneMarkedPinYinField.setBorder( AddToneMarks.compoundBorder );
         }
        return toneMarkedPinYinField;
    }

    /***************************************************************************
     *
     * @param args
     *
     **************************************************************************/

    public static void main( String[] args ) {

        // TODO Auto-generated method stub

        SwingUtilities.invokeLater( new Runnable() {

            public void run() {

                AddToneMarks application = new AddToneMarks();
                application.getJFrame().setVisible( true );
            }
        } );
    }

    /**
     * This method initializes jFrame
     *
     * @return javax.swing.JFrame
     */
    private JFrame getJFrame() {

        if (jFrame == null) {
            jFrame = new JFrame();
            jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            jFrame.setJMenuBar( getjMenuBar() );
            jFrame.setSize( 550, 400 );
            jFrame.setContentPane( getJContentPane() );
            jFrame.setTitle( "PinYin Tone Marker" );
        }
        return jFrame;
    }

    /**
     * This method initializes jContentPane
     *
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {

        if (jContentPane == null) {
            GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
            gridBagConstraints1.fill = GridBagConstraints.BOTH;
            gridBagConstraints1.gridx = 1;
            gridBagConstraints1.gridy = 0;
            gridBagConstraints1.gridwidth = 2;
            gridBagConstraints1.insets = new Insets( 4, 4, 4, 4 );
            gridBagConstraints1.gridheight = 1;
            GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
            gridBagConstraints2.fill = GridBagConstraints.BOTH;
            gridBagConstraints2.gridx = 1;
            gridBagConstraints2.gridy = 2;
            gridBagConstraints2.gridwidth = 2;
            gridBagConstraints2.insets = new Insets( 4, 4, 4, 4 );
            gridBagConstraints2.gridheight = 1;
            jContentPane = new JPanel();
            jContentPane.setLayout( new GridBagLayout() );
            jContentPane.add(getDataPanel(), gridBagConstraints1);
            jContentPane.add( getButtonPanel(), gridBagConstraints2 );
        }
        return jContentPane;
    }

    /**
     * This method initializes jMenuBar
     *
     * @return javax.swing.JMenuBar
     */
    private JMenuBar getjMenuBar() {

        if (jMenuBar == null) {
            jMenuBar = new JMenuBar();
            jMenuBar.add( getFileMenu() );
            jMenuBar.add( getEditMenu() );
            jMenuBar.add( getHelpMenu() );
        }
        return jMenuBar;
    }

    /**
     * This method initializes jMenu
     *
     * @return javax.swing.JMenu
     */
    private JMenu getFileMenu() {

        if (fileMenu == null) {
            fileMenu = new JMenu();
            fileMenu.setText( "File" );
            fileMenu.add( getSaveMenuItem() );
            fileMenu.add( getExitMenuItem() );
        }
        return fileMenu;
    }

    /**
     * This method initializes jMenu
     *
     * @return javax.swing.JMenu
     */
    private JMenu getEditMenu() {

        if (editMenu == null) {
            editMenu = new JMenu();
            editMenu.setText( "Edit" );
            editMenu.add( getCutMenuItem() );
            editMenu.add( getCopyMenuItem() );
            editMenu.add( getPasteMenuItem() );
        }
        return editMenu;
    }

    /**
     * This method initializes jMenu
     *
     * @return javax.swing.JMenu
     */
    private JMenu getHelpMenu() {

        if (helpMenu == null) {
            helpMenu = new JMenu();
            helpMenu.setText( "Help" );
            helpMenu.add( getAboutMenuItem() );
        }
        return helpMenu;
    }

    /**
     * This method initializes jMenuItem
     *
     * @return javax.swing.JMenuItem
     */
    private JMenuItem getExitMenuItem() {

        if (exitMenuItem == null) {
            exitMenuItem = new JMenuItem();
            exitMenuItem.setText( "Exit" );
            exitMenuItem.addActionListener( new ActionListener() {

                public void actionPerformed( ActionEvent e ) {

                    System.exit( 0 );
                }
            } );
        }
        return exitMenuItem;
    }

    /**
     * This method initializes jMenuItem
     *
     * @return javax.swing.JMenuItem
     */
    private JMenuItem getAboutMenuItem() {

        if (aboutMenuItem == null) {
            aboutMenuItem = new JMenuItem();
            aboutMenuItem.setText( "About" );
            aboutMenuItem.addActionListener( new ActionListener() {

                public void actionPerformed( ActionEvent e ) {

                    JDialog aboutDialog = getAboutDialog();
                    aboutDialog.pack();
                    Point loc = getJFrame().getLocation();
                    loc.translate( 20, 20 );
                    aboutDialog.setLocation( loc );
                    aboutDialog.setVisible( true );
                }
            } );
        }
        return aboutMenuItem;
    }

    /**
     * This method initializes aboutDialog
     *
     * @return javax.swing.JDialog
     */
    private JDialog getAboutDialog() {

        if (aboutDialog == null) {
            aboutDialog = new JDialog( getJFrame(), true );
            aboutDialog.setTitle( "About" );
            aboutDialog.setContentPane( getAboutContentPane() );
        }
        return aboutDialog;
    }

    /**
     * This method initializes aboutContentPane
     *
     * @return javax.swing.JPanel
     */
    private JPanel getAboutContentPane() {

        if (aboutContentPane == null) {
            aboutContentPane = new JPanel();
            aboutContentPane.setLayout( new BorderLayout() );
            aboutContentPane.add( getAboutVersionLabel(), BorderLayout.CENTER );
        }
        return aboutContentPane;
    }

    /**
     * This method initializes aboutVersionLabel
     *
     * @return javax.swing.JLabel
     */
    private JLabel getAboutVersionLabel() {

        if (aboutVersionLabel == null) {
            aboutVersionLabel = new JLabel();
            aboutVersionLabel.setText( "Version 1.0" );
            aboutVersionLabel.setHorizontalAlignment( SwingConstants.CENTER );
        }
        return aboutVersionLabel;
    }

    /**
     * This method initializes jMenuItem
     *
     * @return javax.swing.JMenuItem
     */
    private JMenuItem getCutMenuItem() {

        if (cutMenuItem == null) {
            cutMenuItem = new JMenuItem();
            cutMenuItem.setText( "Cut" );
            cutMenuItem
                            .setAccelerator( KeyStroke
                                            .getKeyStroke( KeyEvent.VK_X,
                                                           Event.CTRL_MASK,
                                                           true ) );
        }
        return cutMenuItem;
    }

    /**
     * This method initializes jMenuItem
     *
     * @return javax.swing.JMenuItem
     */
    private JMenuItem getCopyMenuItem() {

        if (copyMenuItem == null) {
            copyMenuItem = new JMenuItem();
            copyMenuItem.setText( "Copy" );
            copyMenuItem
                            .setAccelerator( KeyStroke
                                            .getKeyStroke( KeyEvent.VK_C,
                                                           Event.CTRL_MASK,
                                                           true ) );
        }
        return copyMenuItem;
    }

    /**
     * This method initializes jMenuItem
     *
     * @return javax.swing.JMenuItem
     */
    private JMenuItem getPasteMenuItem() {

        if (pasteMenuItem == null) {
            pasteMenuItem = new JMenuItem();
            pasteMenuItem.setText( "Paste" );
            pasteMenuItem.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_V,
                                                           Event.CTRL_MASK,
                                                           true ) );
            pasteMenuItem.addActionListener( new ActionListener() {

                public void actionPerformed( ActionEvent e ) {

                    GraphicsEnvironment gEnv =
                        GraphicsEnvironment.getLocalGraphicsEnvironment();
                    String envfonts[] = gEnv.getAvailableFontFamilyNames();

                    for ( int i = 1; i < envfonts.length; i++ ) {
                        System.out.println( envfonts[i] );
                    }

                }

            } );

          }

        return pasteMenuItem;
    }

    /**
     * This method initializes jMenuItem
     *
     * @return javax.swing.JMenuItem
     */
    private JMenuItem getSaveMenuItem() {

        if (saveMenuItem == null) {
            saveMenuItem = new JMenuItem();
            saveMenuItem.setText( "Save" );
            saveMenuItem
                            .setAccelerator( KeyStroke
                                            .getKeyStroke( KeyEvent.VK_S,
                                                           Event.CTRL_MASK,
                                                           true ) );
        }
        return saveMenuItem;
    }

    private final class ButtonAction implements java.awt.event.ActionListener {

        private Vector<String> toneMarkedPinYin;
        private String   displayToneMarkedPinYin = "";

        public void actionPerformed( java.awt.event.ActionEvent e ) {

            String numberedPinYin = numberedPinYinField.getText();

            FormatPinYin fpy = new FormatPinYin();
            toneMarkedPinYin = FormatPinYin.addToneMarks( numberedPinYin );

            System.out.println( "In: " + numberedPinYin );
            System.out.println( "Out: " );

            displayToneMarkedPinYin = "";

            for ( String word : toneMarkedPinYin ) {

                System.out.printf( "\"%s\"\n", word );

                displayToneMarkedPinYin = displayToneMarkedPinYin + word;

            }

            //toneMarkedPinYinField.setText( null );

            //textLength = toneMarkedPinYinField.getDocument().getLength();
            //toneMarkedPinYinField.replaceRange( displayToneMarkedPinYin, 0, textLength - 1 );

            toneMarkedPinYinField.setText( displayToneMarkedPinYin.trim() );

            System.out.println( "actionPerformed()" );

         }
    }


}
